<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package}.${moduleName}.mapper.${ClassName}Mapper">

    <resultMap id="${className}BaseResult" type="${package}.${moduleName}.entity.${ClassName}Entity">
        <#list fieldList as field>
        <#if field.primaryPk>
        <id property="${field.attrName}" column="${field.fieldName}"/>
        </#if>
        </#list>
        <#list fieldList as field>
        <#if !field.primaryPk>
        <result property="${field.attrName}" column="${field.fieldName}"/>
        </#if>
        </#list>
    </resultMap>

    <sql id="${className}BaseColumn">
    <#list fieldList as field>
        <#if field_index==0>
        t.${field.fieldName},<#rt/>
        <#elseif field_has_next>
        <#lt/> t.${field.fieldName},<#rt/>
        <#else>
        <#lt/> t.${field.fieldName}
        </#if>
    </#list>
    </sql>

    <insert id="add" parameterType="${package}.${moduleName}.entity.${ClassName}Entity">
        insert into
            ${tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <#list fieldList as field>
            <if test="${field.attrName} != null">
                ${field.fieldName},
            </if>
            </#list>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <#list fieldList as field>
            <if test="${field.attrName} != null">
                <#noparse>#{</#noparse>${field.attrName}<#noparse>}</#noparse>,
            </if>
            </#list>
        </trim>
    </insert>

    <delete id="delete" parameterType="String">
        update
            ${tableName} t
        set
            t.is_used = '0',
            t.update_time = <#noparse>'${@</#noparse>com.util.DateUtil@getDefaultFmtNow()<#noparse>}'</#noparse>,
            t.update_user = <#noparse>'${@</#noparse>com.base.springinit.InitHttpServlet@getSessionUserId()<#noparse>}'</#noparse>
        where
            t.id = <#noparse>#{_parameter}</#noparse>
    </delete>

    <delete id="deleteRealById" parameterType="String">
        delete from
            ${tableName}
        where
            id = <#noparse>#{_parameter}</#noparse>
    </delete>

    <delete id="deleteByIdList" parameterType="java.util.List">
        update
            ${tableName} t
        set
            t.is_used = '0',
            t.update_time = <#noparse>'${@</#noparse>com.util.DateUtil@getDefaultFmtNow()<#noparse>}'</#noparse>,
            t.update_user = <#noparse>'${@</#noparse>com.base.springinit.InitHttpServlet@getSessionUserId()<#noparse>}'</#noparse>
        where
            t.id in
        <foreach collection="list" item="idItem" index="index" open="(" close=")" separator=",">
            <#noparse>#{idItem}</#noparse>
        </foreach>
    </delete>

    <delete id="deleteRealByIdList" parameterType="java.util.List">
        delete from
            ${tableName}
        where
            id in
        <foreach collection="list" item="idItem" index="index" open="(" close=")" separator=",">
            <#noparse>#{idItem}</#noparse>
        </foreach>
    </delete>

    <delete id="deleteByIdArray" parameterType="java.util.List">
        update
            ${tableName} t
        set
            t.is_used = '0',
            t.update_time = <#noparse>'${@</#noparse>com.util.DateUtil@getDefaultFmtNow()<#noparse>}'</#noparse>,
            t.update_user = <#noparse>'${@</#noparse>com.base.springinit.InitHttpServlet@getSessionUserId()<#noparse>}'</#noparse>
        where
            t.id in
        <foreach collection="array" item="idItem" index="index" open="(" close=")" separator=",">
            <#noparse>#{idItem}</#noparse>
        </foreach>
    </delete>

    <delete id="deleteRealByIdArray" parameterType="java.util.List">
        delete from
            ${tableName}
        where
            id in
        <foreach collection="array" item="idItem" index="index" open="(" close=")" separator=",">
            <#noparse>#{idItem}</#noparse>
        </foreach>
    </delete>

    <update id="update" parameterType="${package}.${moduleName}.entity.${ClassName}Entity">
        update
            ${tableName} t
        <set>
            <#list fieldList as field>
            <#if field.attrName != 'createUser' && field.attrName != 'createTime' && field.attrName != 'updateTime' && field.attrName != 'updateUser'>
            <if test="${field.attrName} != null">
                t.${field.fieldName} = <#noparse>#{</#noparse>${field.attrName}<#noparse>}</#noparse>,
            </if>
            </#if>
            </#list>
            t.update_time = <#noparse>'${@</#noparse>com.util.DateUtil@getDefaultFmtNow()<#noparse>}'</#noparse>,
            t.update_user = <#noparse>'${@</#noparse>com.base.springinit.InitHttpServlet@getSessionUserId()<#noparse>}'</#noparse>
        </set>
        where
            id = <#noparse>#{id}</#noparse>
    </update>

    <update id="updateNotEmpty" parameterType="${package}.${moduleName}.entity.${ClassName}Entity">
        update
            ${tableName} t
        <set>
            <#list fieldList as field>
            <#if field.attrName != 'createUser' && field.attrName != 'createTime' && field.attrName != 'updateTime' && field.attrName != 'updateUser'>
            <#if field.attrType == "String">
            <if test="${field.attrName} != null and ${field.attrName} != ''">
            <#else>
            <if test="${field.attrName} != null">
             </#if>
                t.${field.fieldName} = <#noparse>#{</#noparse>${field.attrName}<#noparse>}</#noparse>,
            </if>
            </#if>
            </#list>
            t.update_time = <#noparse>'${@</#noparse>com.util.DateUtil@getDefaultFmtNow()<#noparse>}'</#noparse>,
            t.update_user = <#noparse>'${@</#noparse>com.base.springinit.InitHttpServlet@getSessionUserId()<#noparse>}'</#noparse>
        </set>
        where
            id = <#noparse>#{id}</#noparse>
    </update>

    <select id="getByIdArray" parameterType="java.util.List" resultMap="${className}BaseResult">
        select
            <include refid="${className}BaseColumn"/>
        from
            ${tableName} t
        where
            t.id in
        <foreach collection="array" item="idItem" index="index" open="(" close=")" separator=",">
            <#noparse>#{idItem}</#noparse>
        </foreach>
    </select>

    <select id="getByIdList" parameterType="java.util.List" resultMap="${className}BaseResult">
        select
            <include refid="${className}BaseColumn"/>
        from
            ${tableName} t
        where
            t.id in
        <foreach collection="list" item="idItem" index="index" open="(" close=")" separator=",">
            <#noparse>#{idItem}</#noparse>
        </foreach>
    </select>

    <select id="getById" parameterType="String" resultMap="${className}BaseResult">
        select
            <include refid="${className}BaseColumn"/>
        from
            ${tableName} t
        where
            t.id = <#noparse>#{_parameter}</#noparse>
    </select>

    <select id="getList" parameterType="java.util.Map" resultMap="${className}BaseResult">
        select
            <include refid="${className}BaseColumn"/>
        from
            ${tableName} t
        <where>
            <#list fieldList as field>
            <#if field.attrType == "String">
            <if test="${field.attrName} != null and ${field.attrName} != ''">
            <#else>
            <if test="${field.attrName} != null">
            </#if>
                and t.${field.fieldName} = <#noparse>#{</#noparse>${field.attrName}<#noparse>}</#noparse>
            </if>
            </#list>
        </where>
        <if test="orderBy != null and orderBy != ''">
            order by <#noparse>${orderBy}</#noparse>
        </if>
    </select>
</mapper>